var regexEmail = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i
var regexPassw = /^([0-9a-zA-Z])+$/;

var stringAlert = "<p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>";
var stringAlertSuccess = "<p><span class='ui-icon ui-icon-circle-check' style='float:left; margin:0 7px 50px 0;'></span>";

function checkLength( o, min, max, message) {
    if ( o.val().length > max || o.val().length < min ) {
        o.addClass( "ui-state-error" );
        warning(message, o );
        return false;
    }
    return true;
}

function checkRegexp(element, regexp, message) {
    if ( !( regexp.test( element.val() ) ) ) {
        element.addClass( "ui-state-error" );
        warning(message, element);
        return false;
    }
    return true;
}

function warning(message, element) {
    $('body').append("<div id='tcc-sglj-warning'></div>");
    $("#tcc-sglj-warning").html(message);

    $("#tcc-sglj-warning").dialog({
        width: 350,       
        modal: true,
        autoOpen: true,
        closeOnEscape: true,
        open: function(){
            $('.ui-dialog-buttonpane').find('button:contains("Ok")').button({
                icons: {
                    primary: 'ui-icon-circle-check'
                }
            });
        },
        buttons: {
            "Ok": function() {
                $(this).dialog('close');                
            }
        }
    });
    
    if(element != null) {
        element.focus();
    }
}

jQuery.fn.maskPhone = function(a){
    v = $(this).val();
    v=v.toString().replace(/\D/g,"");
    v=v.toString().replace(/(\d{4})(\d)/,"$1-$2");
    v=v.toString().replace(/(\d{4})(\d)/,"$1-$2");
    $(this).val(v);
    return true;
}

jQuery.fn.maskCPF = function(a){
    v = $(this).val();
    var max = 13;
    v = v.toString().substring (0,max);
    v=v.toString().replace(/\D/g,"");
    v=v.toString().replace(/(\d{3})(\d)/,"$1.$2");
    v=v.toString().replace(/(\d{3})(\d)/,"$1.$2");
    v=v.toString().replace(/(\d{3})(\d{1,2})$/,"$1-$2");
    $(this).val(v);
    return true;
}

jQuery.fn.clearCPF=function(a){
    var b=this;
    var c=b.val().replace('.','').replace('.','').replace('-','');
    return c;
}

jQuery.fn.validateCPF=function(a){
    var b=this;
    var c=b.val().replace('.','').replace('.','').replace('-','');
    if(c.length!=11){
        return false
    }else{
        var d=/1{11}|2{11}|3{11}|4{11}|5{11}|6{11}|7{11}|8{11}|9{11}|0{11}/;
        if(d.test(c)){
            return false
        }else{
            var x=0;
            var y=0;
            var e=0;
            var f=0;
            var g=0;
            var h="";
            var k="";
            var l=c.length;
            x=l-1;
            for(var i=0;i<=l-3;i++){
                y=c.substring(i,i+1);
                e=e+(y*x);
                x=x-1;
                h=h+y
            }
            f=11-(e%11);
            if(f==10){
                f=0
            }
            if(f==11){
                f=0
            }
            k=c.substring(0,l-2)+f;
            x=11;
            e=0;
            for(var j=0;j<=(l-2);j++){
                e=e+(k.substring(j,j+1)*x);
                x=x-1
            }
            g=11-(e%11);
            if(g==10){
                g=0
            }
            if(g==11){
                g=0
            }
            if((f+""+g)==c.substring(l,l-2)){
                return true
            }else{
                return false
            }
        }
    }
};

$.extend($.ui.dialog.prototype, {
    'addbutton': function(buttonName, func) {
        var buttons = this.element.dialog('option', 'buttons');
        buttons[buttonName] = func;
        this.element.dialog('option', 'buttons', buttons);
    }
});

$.extend($.ui.dialog.prototype, {
    'removebutton': function(buttonName) {
        var buttons = this.element.dialog('option', 'buttons');
        delete buttons[buttonName];
        this.element.dialog('option', 'buttons', buttons);
    }
});


function loading(action){
    if (action) {
        $(".ajax_load_gif").remove();
        var span = "margin-top: 15px;" +
        "float:right;" +
        "margin-right: 10px;" +
        "display:none;";
        $(".ui-dialog-buttonpane").append("<img src='./jdock/images/ajax-loader.gif' class='ajax_load_gif' style='" + span + "'>");
        $(".ajax_load_gif").fadeIn('1000');
    } else {
        $(".ajax_load_gif").fadeOut('1000');
    }
}